﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Code.DAL;
using System.Data;

/// <summary>
///ColType 的摘要说明
/// </summary>
public class ColType
{
    public static string GetColType(Column col)
    {
        string[] strString = new string[] { "char", "nchar", "ntext", "nvarchar", "text", "varchar" };
        string[] strGuid = new string[] { "uniqueidentifier" };
        string[] strLong = new string[] { "bigint" };
        string[] strInt = new string[] { "int", "smallint", "tinyint" };
        string[] strDouble = new string[] { "decimal", "float", "money", "real", "smallmoney", "numeric" };
        string[] strDateTime = new string[] { "smalldatetime", "datetime" };
        string[] strBytes = new string[] { "timestamp", "varbinary", "binary" };
        string[] strBool = new string[] { "bit" };
        string colName = col.Type.ToLower();
        if (strString.Contains(colName))
        {
            return "string";
        }
        if (strGuid.Contains(colName))
        {
            return col.IsNull?"Guid?":"Guid";
        }
        if (strLong.Contains(colName))
        {
            return col.IsNull ? "long?" : "long";
        }
        if (strInt.Contains(colName))
        {
            return col.IsNull ? "int?" : "int";
        }
        if (strDouble.Contains(colName))
        {
            return col.IsNull ? "decimal?" : "decimal";
        }
        if (strDateTime.Contains(colName))
        {
            return col.IsNull ? "DateTime?" : "DateTime";
        }
        if (strBytes.Contains(colName))
        {
            return "byte []";
        }
        if (strBool.Contains(colName))
        {
            return  col.IsNull ? "bool?":"bool";
        }
        return "没有找到合适的类型";
    }



    public static string GetSQLDBType(string typeName)
    {
        switch (typeName.ToLower())
        {
            case ("bigint"):
                return "SqlDbType.BigInt";
            case ("binary"):
                return "SqlDbType.Binary";
            case ("bit"):
                return "SqlDbType.Bit";
            case ("char"):
                return "SqlDbType.Char";
            case ("datetime"):
                return "SqlDbType.DateTime";
            case ("decimal"):
                return "SqlDbType.Decimal";
            case ("float"):
                return "SqlDbType.Float";
            case ("image"):
                return "SqlDbType.Image";
            case ("int"):
                return "SqlDbType.Int";
            case ("money"):
                return "SqlDbType.Money";
            case ("nchar"):
                return "SqlDbType.NChar";
            case ("ntext"):
                return "SqlDbType.NText";
            case ("nvarchar"):
                return "SqlDbType.NVarChar";
            case ("real"):
                return "SqlDbType.Real";
            case ("smalldatetime"):
                return "SqlDbType.SmallDateTime";
            case ("smallint"):
                return "SqlDbType.SmallInt";
            case ("smallmoney"):
                return "SqlDbType.SmallMoney";
            case ("text"):
                return "SqlDbType.Text";
            case ("timestamp"):
                return "SqlDbType.Timestamp";
            case ("tinyint"):
                return "SqlDbType.TinyInt";
            case ("varbinary"):
                return "SqlDbType.VarBinary";
            case ("varchar"):
                return "SqlDbType.VarChar";
            case ("numeric"):
                return "SqlDbType.Decimal";
            case ("uniqueidentifier"):
                return "SqlDbType.UniqueIdentifier";
            default:
                return "没有找到数据类型";
        }
    }
}